package com.b511.swpu.cementbackend.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.util.HashSet;
import java.util.Set;

@Slf4j
public class CopyUtil {
    private static String[] getIgnorePropertyNames(Object source) {
        BeanWrapper from = new BeanWrapperImpl(source);
        Set<String> ignorePropertyNames = new HashSet<>();
        for (java.beans.PropertyDescriptor pd : from.getPropertyDescriptors()) {
            if (null == from.getPropertyValue(pd.getName())) {
                ignorePropertyNames.add(pd.getName());
            }
        }
        return ignorePropertyNames.toArray(new String[0]);
    }

    public static void copyProperties(Object from, Object target) {
        BeanUtils.copyProperties(from, target, getIgnorePropertyNames(from));
    }
}
